package org.jeecg.modules.zdh.controller;

import java.util.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.until.Sm4Util;
import org.jeecg.modules.zdh.entity.Chry;
import org.jeecg.modules.zdh.entity.Wdhy;
import org.jeecg.modules.zdh.service.IChryService;
import org.jeecg.modules.zdh.service.IWdhyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 我的会议
 * @Author: jeecg-boot
 * @Date:   2023-12-19
 * @Version: V1.0
 */
@Slf4j
@Api(tags="我的会议")
@RestController
@RequestMapping("/zdh/wdhy")
public class WdhyController extends JeecgController<Wdhy, IWdhyService> {
	@Autowired
	private IWdhyService wdhyService;
	@Autowired
	private IChryService chryService;
	
	/**
	 * 分页列表查询
	 *
	 * @param wdhy
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "我的会议-分页列表查询")
	@ApiOperation(value="我的会议-分页列表查询", notes="我的会议-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Wdhy wdhy,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Wdhy> queryWrapper = QueryGenerator.initQueryWrapper(wdhy, req.getParameterMap());
		Page<Wdhy> page = new Page<Wdhy>(pageNo, pageSize);
		IPage<Wdhy> pageList = wdhyService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 我的会议加密
	  *
	  * @return
	  */
	 @GetMapping(value = "/wdhyEncrypt")
	 public Result<?> wdhyEncrypt() {
		 List<Wdhy> wdhyList = wdhyService.list();
		 for (Wdhy wdhy:wdhyList) {
			 wdhy.set姓名(Sm4Util.SM4encrypt(wdhy.get姓名decrypt()));
			 wdhy.set手机号(Sm4Util.SM4encrypt(wdhy.get手机号decrypt()));
			 wdhy.set会议名称(Sm4Util.SM4encrypt(wdhy.get会议名称decrypt()));
			 wdhy.set会议类型(Sm4Util.SM4encrypt(wdhy.get会议类型decrypt()));
			 wdhy.set会议地点(Sm4Util.SM4encrypt(wdhy.get会议地点decrypt()));
			 wdhy.set会议日期(Sm4Util.SM4encrypt(wdhy.get会议日期decrypt()));
			 wdhy.set会议时间(Sm4Util.SM4encrypt(wdhy.get会议时间decrypt()));
			 wdhy.set会议座序(Sm4Util.SM4encrypt(wdhy.get会议座序decrypt()));
			 wdhyService.updateById(wdhy);
		 }
		 return Result.OK("编辑成功!");
	 }

	 @GetMapping(value = "/insertWdhy")
	 public Result<?> insertWdhy() {
		 QueryWrapper<Chry> queryWrapper =new QueryWrapper<Chry>();
		 queryWrapper.eq("职代会","1");
		 List<Chry> chryList = chryService.list(queryWrapper);

		 QueryWrapper<Wdhy> queryWrapper2 =new QueryWrapper<Wdhy>();
		 queryWrapper.eq("手机号decrypt","18690214367");
		 queryWrapper.eq("会议类型decrypt","职代会");
		 List<Wdhy> wdhyList = wdhyService.list();

		 List<Wdhy> arr = new ArrayList<Wdhy>();
		 for (Chry chry : chryList) {
			 if(chry.get手机号decrypt().equals("18690214367")){
				 continue;
			 }
			 for (Wdhy wdhy: wdhyList) {
				 wdhy.setId(null);
			 	 wdhy.set姓名decrypt(chry.get姓名decrypt());
				 wdhy.set手机号decrypt(chry.get手机号decrypt());
				 if(wdhy.get会议名称decrypt().equals("分团讨论")){
					 wdhy.set会议地点decrypt(get会议地点(chry.get代表团decrypt()));
				 }
//				 arr.add(wdhy);
				 wdhyService.save(wdhy);
			 }
		 }
//		 wdhyService.saveBatch(arr);
		 return Result.OK("编辑成功!");
	 }
	 public String get会议地点(String 代表团){
		 if(代表团.equals("第一代表团")){
		 	 return "党校(教育学院)综合楼307室";
		 }else if (代表团.equals("第二代表团")){
			 return "党校(教育学院)综合楼306室";
		 }else if (代表团.equals("第三代表团")){
			 return "党校(教育学院)综合楼305室";
		 }else if (代表团.equals("第四代表团")){
			 return "党校(教育学院)综合楼206室";
		 }else if (代表团.equals("第五代表团")){
			 return "党校(教育学院)综合楼207室";
		 }else if (代表团.equals("第六代表团")){
			 return "党校(教育学院)综合楼209室";
		 }else if (代表团.equals("第七代表团")){
			 return "党校(教育学院)综合楼210室";
		 }else if (代表团.equals("第八代表团")){
			 return "党校(教育学院)综合楼211室";
		 }else if (代表团.equals("第九代表团")){
			 return "党校(教育学院)综合楼212室";
		 }else if (代表团.equals("第十代表团")){
			 return "党校(教育学院)综合楼214室";
		 }else if (代表团.equals("第十一代表团")){
			 return "党校(教育学院)综合楼215室";
		 }
		 return "";
	 }



	/**
	 * 添加
	 *
	 * @param wdhy
	 * @return
	 */
	@AutoLog(value = "我的会议-添加")
	@ApiOperation(value="我的会议-添加", notes="我的会议-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Wdhy wdhy) {
		wdhyService.save(wdhy);
		return Result.OK("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param wdhy
	 * @return
	 */
	@AutoLog(value = "我的会议-编辑")
	@ApiOperation(value="我的会议-编辑", notes="我的会议-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<?> edit(@RequestBody Wdhy wdhy) {
		wdhyService.updateById(wdhy);
		return Result.OK("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "我的会议-通过id删除")
	@ApiOperation(value="我的会议-通过id删除", notes="我的会议-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		wdhyService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "我的会议-批量删除")
	@ApiOperation(value="我的会议-批量删除", notes="我的会议-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.wdhyService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "我的会议-通过id查询")
	@ApiOperation(value="我的会议-通过id查询", notes="我的会议-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Wdhy wdhy = wdhyService.getById(id);
		return Result.OK(wdhy);
	}

	 @GetMapping(value = "/queryByPhone")
	 public Result<?> queryByPhone(@RequestParam(name="phone",required=true) String phone,@RequestParam(name="hylx",required=true) String hylx) {
		 QueryWrapper<Wdhy> queryWrapper = new QueryWrapper<Wdhy>();
		 queryWrapper.eq("手机号",phone);
		 queryWrapper.eq("会议类型",hylx);
		 List<Wdhy> wdhyList = wdhyService.list(queryWrapper);
		 return Result.OK(wdhyList);
	 }
  /**
   * 导出excel
   *
   * @param request
   * @param wdhy
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, Wdhy wdhy) {
      return super.exportXls(request, wdhy, Wdhy.class, "我的会议");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, Wdhy.class);
  }

  @GetMapping(value = "/importZXT")
  public Result<?> importZXT() {
//	 return super.importExcel(request, response, Wdhy.class);
//	  ExcelImportUtil.
	  ExcelReader reader = ExcelUtil.getReader("g:/作序图/职代会座序图20240110_lcg.xls");
	  int index = 1;
	  List<Map<String, Object>> readAll = reader.readAll();
	  for (Map<String,Object> map: readAll) {
			for(String key :map.keySet()){
//				Console.log(key+":"+map.get(key));
				if(!key.equals("排") && !ObjectUtil.hasNull(map.get(key))){
					String 姓名 =  map.get(key).toString();
					String 座序= StrUtil.format("{}{}座", map.get("排"), key);
					Console.log(StrUtil.format("{}:{}", 姓名, 座序));
					if(!StrUtil.hasEmpty(姓名)){
						UpdateWrapper<Wdhy> updateWrapper = new UpdateWrapper<Wdhy>();
						updateWrapper.eq("姓名decrypt",姓名);
						updateWrapper.eq("会议类型decrypt","职代会");
						updateWrapper.eq("会议名称decrypt","预备会");
						updateWrapper.set("会议座序decrypt",座序);
						wdhyService.update(updateWrapper);
					}
				}

			}
		  index++;
	  }
	  //Console.log(readAll);
	return  Result.OK();
  }
}
